import plugin from '../../lib/plugins/plugin.js'
import { segment } from "oicq";
import fetch from 'node-fetch'
import cfg from '../../lib/config/config.js'

export class example extends plugin {
  constructor () {
    super({
      /** 功能名称 */
      name: '涩图',
      /** 功能描述 */
      dsc: '简单开发示例',
      /** https://oicqjs.github.io/oicq/#events */
      event: 'message',
      /** 优先级，数字越小等级越高 */
      priority: -9999,
      rule: [
        {
          /** 命令正则匹配 */
          reg: '^#acg$',
          /** 执行方法 */
          fnc: 'setu'
        },
        {
          /** 命令正则匹配 */
          reg: '^#acg搜索.*$',
          /** 执行方法 */
          fnc: 'setuss'
        },
      ]
      
    })
  }

 
  async setu(e) {
   
  let msg;
  let proxy = "i.pixiv.re"; //代理pixiv.runrab.workers.dev
  let size = "original"; //original,regular,small,thumb,mini,图片质量
  let r18 = "0";//r18模式 0关闭1开启  R18模式会漏点
  let timeout = 10000; //0表示不撤回，单位毫秒
  let lolicon = `https://api.lolicon.app/setu/v2?proxy=${proxy}&size=${size}&r18=${r18}`;//&tag=${keyword}
  //let lolicon = `https://pixiv.loliapi.ml/api`;
  
  let pid = null;
  let title = null;
  try{
    let response = await fetch(lolicon);
    let res = await response.json(); 
    let data = res.data[0];
    let url = data.urls[size].replace("540x540_70","600x600");
    pid = data.pid;
    title = data.title;
  msg = [
    segment.at(e.user_id),
    "\n",segment.image(url),  //image是图片 flash是闪照
    "\n",`PID:${pid}`,
    "\n",`标题:${title}`,
  ];
  } catch(err) {
    msg = [
      segment.at(e.user_id),"\n",
      err.message,"\n",
      "错误！请重试！",
    ];
  }
  let msgRes = await e.reply(msg);
  if (timeout!=0 && msgRes && msgRes.message_id){
    let target = null;
    if (e.isGroup) {
      target = e.group;
    }else{
      target = e.friend;
    }
    if (target != null){
      setTimeout(() => {
        target.recallMsg(msgRes.message_id);
        target.recallMsg(e.message_id);
      }, timeout);
    }
  }  
  return true; //返回true 阻挡消息不再往下
  }
  
  async setuss(e){
      let msg;
      let keyword = e.msg.replace(/#|acg搜索/g,"").trim()
      let proxy = "i.pixiv.re"; //代理
  let size = "small"; //original,regular,small,thumb,mini,图片质量
  let r18 = "0";//r18模式 0关闭1开启
  let timeout = 10000; //0表示不撤回，单位毫秒
  let lolicon = `https://api.lolicon.app/setu/v2?proxy=${proxy}&size=${size}&r18=${r18}&tag=${keyword}`;
 
  
  let pid = null;
  let title = null;
  try{
    let response = await fetch(lolicon);
    let res = await response.json(); 
    let data = res.data[0];
    let url = data.urls[size].replace("540x540_70","600x600");
    pid = data.pid;
    title = data.title;
  msg = [
    segment.at(e.user_id),
    "\n",segment.flash(url),  //image是图片 flash是闪照
    "\n",`PID:${pid}`,
    "\n",`标题:${title}`,
  ];
  } catch(err) {
    msg = [
      segment.at(e.user_id),"\n",
      err.message,"\n",
      "错误！请重试！",
    ];
  }
  let msgRes = await e.reply(msg);
  if (timeout!=0 && msgRes && msgRes.message_id){
    let target = null;
    if (e.isGroup) {
      target = e.group;
    }else{
      target = e.friend;
    }
    if (target != null){
      setTimeout(() => {
        target.recallMsg(msgRes.message_id);
        target.recallMsg(e.message_id);
      }, timeout);
    }
  }  
  return true; //返回true 阻挡消息不再往下
  }
}
